package com.imay.live.update;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.imay.base.fragment.BaseDialogFragment;
import com.imay.live.R;
import com.imay.live.activity.MainActivity2;
import com.imay.live.utils.ToastUtil;

/**
 * Created by lzx on 2017/4/28.
 */

public class UpdateAppFragment extends BaseDialogFragment implements View.OnClickListener {
    private TextView dialog_ok, dialog_cancle, dialog_title;
    public static String TITLE = "title";
    private LinearLayout update_linear;
    private AppUpdateModel appUpdateModel;

    public static UpdateAppFragment newInstance() {
        return new UpdateAppFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.updateapp_layout;
    }

    public AppUpdateModel getAppUpdateModel() {
        return appUpdateModel;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        dialog_ok = $(R.id.dialog_ok);
        dialog_cancle = $(R.id.dialog_cancle);
        update_linear = $(R.id.update_linear);
        dialog_title = $(R.id.dialog_title);
        dialog_ok.setOnClickListener(this);
        dialog_cancle.setOnClickListener(this);
        Bundle bundle = getArguments();
        appUpdateModel = bundle.getParcelable(TITLE);
        if (appUpdateModel == null) {
            dismissAllowingStateLoss();
            getActivity().finish();
            return;
        }
//        String title = "80%\t的用户正在使用新版本";
        if (!TextUtils.isEmpty(appUpdateModel.getTitle())) {
            SpannableString spanstring = new SpannableString(appUpdateModel.getTitle());
            int index = appUpdateModel.getTitle().indexOf("%");
            if (index > -1) {
                spanstring.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.color_ff)), 0, index + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            }
            dialog_title.setText(spanstring);
        }


        if (!TextUtils.isEmpty(appUpdateModel.getDesc1())) {
            addContentView(appUpdateModel.getDesc1());
        }
        if (!TextUtils.isEmpty(appUpdateModel.getDesc2())) {
            addContentView(appUpdateModel.getDesc2());
        }
        if (!TextUtils.isEmpty(appUpdateModel.getDesc3())) {
            addContentView(appUpdateModel.getDesc3());
        }
        getDialog().setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                ((MainActivity2) getActivity()).onClick(dialog_cancle);
            }
        });
    }

    private void addContentView(String content) {
        TextView textView = new TextView(getContext());
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.bottomMargin = (int) getResources().getDimension(R.dimen.size_7dp);
        textView.setText(content);
        textView.setTextSize(13);
        textView.setLineSpacing(6, 1);
        textView.setTextColor(getResources().getColor(R.color.text_4a));
        update_linear.addView(textView, params);
    }

    @Override
    protected boolean isMatchParent() {
        return false;
    }

    @Override
    public void onClick(View v) {
        if (getActivity() instanceof MainActivity2) {
            ((MainActivity2) getActivity()).onClick(v);
            ((MainActivity2) getActivity()).showSomeThing();
        }
        if (v.getId() == R.id.dialog_ok) {
//            v.setTag("https://iosapp.imay.cn/imay_live.apk");
            ToastUtil.show(getContext(), getResources().getString(R.string.str_downtips));
            dismissAllowingStateLoss();
        } else if (v.getId() == R.id.dialog_cancle) {
            dismissAllowingStateLoss();
        }

    }
}
